package org.itboys.admin.service.lasted;


import org.itboys.admin.dao.SundayAreaMapper;
import org.itboys.admin.dto.lasted.SundayAreaDto;
import org.itboys.admin.entity.lasted.SundayArea;
import org.itboys.admin.entity.lasted.SystemDictionary;
import org.itboys.admin.entity.lasted.SystemMenu;
import org.itboys.admin.entity.lasted.SystemUser;
import org.itboys.commons.utils.encryption.Digests;
import org.itboys.commons.utils.tools.ListUtil;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：jiangxiong
 * 日期：2017年2月8日
 * 联系方式 ：empty90911@126.com
 * 描述：系统_实例化系统菜单_熊熊自定义
 *
 */
public class InitSystemDataService implements InitializingBean {

	@Autowired
	private SystemUserService userService;
	@Autowired
	private SystemMenuService menuService;
	@Autowired
	private SundayAreaMapper areaMapper;
	@Autowired
	private SystemDictionaryService dictionaryService;

	public  String username;//用户名
	//	系统管理
	private String systemMenuUrl; //系统管理url
	private String menuManageUrl;//菜单管理url
	private String userManageUrl;//用户管理url
	private String orgManageUrl;//组织管理url
	private String roleManageUrl;//角色管理url
	private String menuOperateUrl;//菜单功能管理URL
	private String processTemplateUrl;//流程模板管理
	private String processUrl;//流程管理
	private String logUrl;//日志管理


	/*private String systemConfigUrl;// 配置管理url*/
	//配置管理
	private String dictionaryManagerUrl;// 数据字典
	private String appManagerUrl;// app管理
	private String payConfigUrl;// 支付配置
	//其他管理
	private String advertisementUrl;// 广告
	//微信管理
	private String weixinConfigUrl;//微信配置管理



	/*private String paramTemplateManagerUrl;//
	private String emailTemplateManagerUrl;//
	private String msgTemplateManagerUrl;//
	private String weixinUrl;*/

	/* 超级用户名  */
	/*private static final String SYSTEM_ROLE_SUPER = username;*/
	/* 顶级菜单名  */
	private static final String SYSTEM_MENU_SUPER = "系统管理";
	/* 配置管理 */
	private static final String SYSTEM_CONFIG_SUPER = "配置管理";
	/* 配置管理 */
	private static final String SYSTEM_OTHER_SUPER = "其他管理";
	/* 微信管理 */
	private static final String  WEIXIN_CONFIG_SUPER = "微信管理";

	private static final String FA_DESKTOP = "fa-desktop";// 系统管理样式
	private static final String FA_COG = "fa-cog";// 配置管理样式
	private static final String FA_CUBES = " fa-cubes";// 其他管理

	private static final String FA_COMMENTS = "fa-comments";// 微信管理样式*/

	@Override
	public void afterPropertiesSet() throws Exception {
		Map<String,Object> param = new HashMap<String,Object>();
		/* 若数据库中没有admin超级用户，自动添加超级管理员   */
		param.put("userName",username);
		param.put("isDeleted",0);
		List<SystemUser> adminUsers=userService.list(param);
		if(adminUsers==null||adminUsers.size()<1){

			SystemUser user = new SystemUser();
			user.setUserName(username);
			user.setName("超级管理员");
			user.setIsSuper(1);
			user.setStatus(0);
			user.setPassword(Digests.md5("111111"));
			user.setIsDeleted(0);

			userService.saveWithoutLogin(user);
		}
		/**
		 * 若数据库中没有基础的字段数据。则自动穿件
		 */
		param.clear();
		/* 若数据库中没有admin超级用户，自动添加超级管理员   */
		//param.put("userName",username);

		String [] keys = new String[]{"boolean","advertisementPageType"};
		param.put("key in",keys);
		//List<SystemDictionary> dictionaries=dictionaryService.list(param);
		if(dictionaryService.count(param)<1){
			List<SystemDictionary> dictionaries =new ArrayList<>();
			SystemDictionary dictionary_boolean1=new SystemDictionary("boolean", "是", "1", "是或否");
			dictionaries.add(dictionary_boolean1);
			SystemDictionary dictionary_boolean2=new SystemDictionary("boolean", "否", "0", "是或否");
			dictionaries.add(dictionary_boolean2);

			SystemDictionary dictionary_ad1=new SystemDictionary("advertisementPageType", "浏览内容", "0", "控制广告在前端时转跳的参数");
			dictionaries.add(dictionary_ad1);
			SystemDictionary dictionary_ad2=new SystemDictionary("advertisementPageType", "商品详情", "1", "控制广告在前端时转跳的参数");
			dictionaries.add(dictionary_ad2);
			SystemDictionary dictionary_ad3=new SystemDictionary("advertisementPageType", "商品列表", "2", "控制广告在前端时转跳的参数");
			dictionaries.add(dictionary_ad3);
			SystemDictionary dictionary_ad4=new SystemDictionary("advertisementPageType", "店铺详情", "3", "控制广告在前端时转跳的参数");
			dictionaries.add(dictionary_ad4);
			dictionaryService.batchSaveWithoutLogin(dictionaries);

		}






		/* 若没有顶级菜单，则自动创建   */
		param.clear();
		param.put("name",SYSTEM_MENU_SUPER);
		List<SystemMenu> systemMenus=menuService.list(param);
		if(systemMenus==null||systemMenus.size()<1){
			//	if(!adminMenuService.exists("menuName", SYSTEM_MENU_SUPER)){
			List<SystemMenu> menuList = new ArrayList<SystemMenu>();
			/* 创建顶级菜单  */
			SystemMenu systemMenu = new SystemMenu(
					SYSTEM_MENU_SUPER, systemMenuUrl, 1,
					0l, FA_DESKTOP, 1, SYSTEM_MENU_SUPER,null);

			menuService.saveWithoutLogin(systemMenu);

			/* 子菜单:菜单管理   */
			SystemMenu menuMenu = new SystemMenu(
					"菜单管理", menuManageUrl, 1,
					systemMenu.getId(), null, 1, "菜单管理","/ace/system/menu");
			menuList.add(menuMenu);
			/* 子菜单:用户管理   */
			SystemMenu userMenu = new SystemMenu(
					"用户管理", userManageUrl, 2,
					systemMenu.getId(), null, 1, "用户管理","/ace/system/user");
			menuList.add(userMenu);
			/* 子菜单:组织管理   */
			SystemMenu orgMenu = new SystemMenu(
					"组织管理", orgManageUrl, 3,
					systemMenu.getId(), null, 1, "组织管理","/ace/system/org");
			menuList.add(orgMenu);
			/* 子菜单:角色管理   */
			SystemMenu roleMenu = new SystemMenu(
					"角色管理", roleManageUrl, 4,
					systemMenu.getId(), null, 1, "角色管理","/ace/system/role");
			menuList.add(roleMenu);
			/* 子菜单:菜单功能管理   */
			SystemMenu menuOperateMenu = new SystemMenu(
					"菜单功能管理", menuOperateUrl, 5,
					systemMenu.getId(), null, 1, "菜单功能管理","/ace/system/menu/operate");
			menuList.add(menuOperateMenu);
			/* 子菜单:流程模板管理   */
			SystemMenu processTemplateMenu = new SystemMenu(
					"流程模板管理", processTemplateUrl, 6,
					systemMenu.getId(), null, 1, "流程模板管理","/ace/system/process/template");
			menuList.add(processTemplateMenu);
			/* 子菜单:流程管理   */
			SystemMenu processMenu = new SystemMenu(
					"流程管理", processUrl, 7,
					systemMenu.getId(), null, 1, "流程管理","/ace/system/process");
			menuList.add(processMenu);
			/* 子菜单:日志管理   */
			SystemMenu logMenu = new SystemMenu(
					"日志管理", logUrl, 8,
					systemMenu.getId(), null, 1, "日志管理","/ace/system/log");
			menuList.add(logMenu);

			menuService.batchSaveWithoutLogin(menuList);
		}

		/* 若没有配置管理菜单，则自动创建   */
		param.clear();
		param.put("name",SYSTEM_CONFIG_SUPER);
		List<SystemMenu> configMenus=menuService.list(param);
		if(configMenus==null||configMenus.size()<1){
/*
		}
		if(!adminMenuService.exists("menuName", SYSTEM_CONFIG_SUPER)){*/
			List<SystemMenu> menuList = new ArrayList<SystemMenu>();
			/* 创建配置管理菜单-顶级菜单  */
			SystemMenu configMenu = new SystemMenu(
					SYSTEM_CONFIG_SUPER, "#", 2,
					0l, FA_COG, 1, SYSTEM_CONFIG_SUPER,null);
			menuService.saveWithoutLogin(configMenu);

			/* 子菜单:数据字典   */
			SystemMenu dicMenu = new SystemMenu(
					"数据字典", dictionaryManagerUrl, 1,
					configMenu.getId(), null, 1, "数据字典","/ace/system/dictionary");
			menuList.add(dicMenu);
			//初始化字典中基本数据


			/**子菜单：app管理**/
			SystemMenu appMenu = new SystemMenu(
					"app管理", appManagerUrl, 2,
					configMenu.getId(), null, 1, "app管理","/sunday/web/apk");
			menuList.add(appMenu);

			/**子菜单：支付配置**/
			SystemMenu payConfigMenu = new SystemMenu(
					"支付配置", payConfigUrl, 3,
					configMenu.getId(), null, 1, "支付配置","/sunday/web/pay/config");
			menuList.add(payConfigMenu);

			menuService.batchSaveWithoutLogin(menuList);




		}
		/* 若没有其他管理菜单，则自动创建   */
		param.clear();
		param.put("name",SYSTEM_OTHER_SUPER);
		List<SystemMenu> otherMenus=menuService.list(param);
		if(otherMenus==null||otherMenus.size()<1){
/*
		}
		if(!adminMenuService.exists("menuName", SYSTEM_CONFIG_SUPER)){*/
			List<SystemMenu> menuList = new ArrayList<SystemMenu>();
			/* 创建其他管理菜单-顶级菜单  */
			SystemMenu otherMenu = new SystemMenu(
					SYSTEM_OTHER_SUPER, "#", 3,
					0l, FA_CUBES, 1, SYSTEM_OTHER_SUPER,null);
			menuService.saveWithoutLogin(otherMenu);

			/* 子菜单:广告管理   */
			SystemMenu dicMenu = new SystemMenu(
					"广告管理", advertisementUrl, 1,
					otherMenu.getId(), null, 1, "广告管理","/sunday/web/advertisement");
			menuList.add(dicMenu);
			menuService.batchSaveWithoutLogin(menuList);

		}
		/* 若没有微信管理菜单，则自动创建   */
		param.clear();
		param.put("name",WEIXIN_CONFIG_SUPER);
		List<SystemMenu> weixinMenus=menuService.list(param);
		if(weixinMenus==null||weixinMenus.size()<1){

			List<SystemMenu> menuList = new ArrayList<SystemMenu>();
			/* 创建微信管理菜单-顶级菜单  */
			SystemMenu weixinConfigMenu = new SystemMenu(
					WEIXIN_CONFIG_SUPER, "#", 4,
					0l, FA_COMMENTS, 1, WEIXIN_CONFIG_SUPER,null);
			menuService.saveWithoutLogin(weixinConfigMenu);

			/* 子菜单:广告管理   */
			SystemMenu dicMenu = new SystemMenu(
					"微信服务号管理", weixinConfigUrl, 1,
					weixinConfigMenu.getId(), null, 1, "微信服务号管理","/sunday/web/weixin");
			menuList.add(dicMenu);
			menuService.batchSaveWithoutLogin(menuList);

		}
		/* 若没有微信配置管理菜单，则自动创建   */
	/*	if(!adminMenuService.exists("menuName", WEIXIN_CONFIG_SUPER)){
			List<SystemMenu> menuList = Lists.newArrayListWithExpectedSize(5);
			*//* 创建配置管理菜单  *//*
			SystemMenu am = new SystemMenu();
			am.setMenuName(WEIXIN_CONFIG_SUPER);
			am.setMenuClass(WEIXIN_COG);
			am.setUrl(systemConfigUrl);
			am.setFullPath(systemConfigUrl);
			am.setParentId(0l);
			am.setLevel(1);
			am.setSort(3);
			adminMenuService.saveWithoutLogin(am);

			*//* 子菜单:微信配置   *//*
			SystemMenu am2 = new SystemMenu();
			am2.setMenuName("微信配置");
			am2.setUrl(weixinUrl);
			am2.setFullPath(weixinUrl);
			am2.setParentId(am.getId());
			am2.setSort(1);
			am2.setLevel(2);
			menuList.add(am2);
			adminMenuService.saveWithoutLogin(menuList);
		}*/
		/**
		 * 2017年5月10日，初始化省市区数据
		 */
		param.clear();
		//获取全部地区
		List<SundayArea> all = areaMapper.select(param);
		//	List<SundayArea> all = new ArrayList<SundayArea>();
		List<SundayArea> allProvinces =new ArrayList<SundayArea>();
		List<SundayArea> allCitys =new ArrayList<SundayArea>();
		List<SundayArea> allDistricts =new ArrayList<SundayArea>();
		List<SundayArea> allTowns =new ArrayList<SundayArea>();
		for(SundayArea a:all){
			if(a.getLevel()==1){
				allProvinces.add(a);
			}
			if(a.getLevel()==2){
				allCitys.add(a);
			}
			if(a.getLevel()==3){
				allDistricts.add(a);
			}
			if(a.getLevel()==4){
				allTowns.add(a);
			}
		}
		SundayAreaService.all_provinces=allProvinces;
		SundayAreaService.all_citys=allCitys;
		SundayAreaService.all_districts=allDistricts;
		SundayAreaService.all_towns=allTowns;

	}

	public SystemUserService getUserService() {
		return userService;
	}

	public void setUserService(SystemUserService userService) {
		this.userService = userService;
	}

	public SystemMenuService getMenuService() {
		return menuService;
	}

	public void setMenuService(SystemMenuService menuService) {
		this.menuService = menuService;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getSystemMenuUrl() {
		return systemMenuUrl;
	}

	public void setSystemMenuUrl(String systemMenuUrl) {
		this.systemMenuUrl = systemMenuUrl;
	}

	public String getMenuManageUrl() {
		return menuManageUrl;
	}

	public void setMenuManageUrl(String menuManageUrl) {
		this.menuManageUrl = menuManageUrl;
	}

	public String getUserManageUrl() {
		return userManageUrl;
	}

	public void setUserManageUrl(String userManageUrl) {
		this.userManageUrl = userManageUrl;
	}

	public String getOrgManageUrl() {
		return orgManageUrl;
	}

	public void setOrgManageUrl(String orgManageUrl) {
		this.orgManageUrl = orgManageUrl;
	}

	public String getRoleManageUrl() {
		return roleManageUrl;
	}

	public void setRoleManageUrl(String roleManageUrl) {
		this.roleManageUrl = roleManageUrl;
	}

	public String getMenuOperateUrl() {
		return menuOperateUrl;
	}

	public void setMenuOperateUrl(String menuOperateUrl) {
		this.menuOperateUrl = menuOperateUrl;
	}

	public String getProcessTemplateUrl() {
		return processTemplateUrl;
	}

	public void setProcessTemplateUrl(String processTemplateUrl) {
		this.processTemplateUrl = processTemplateUrl;
	}

	public String getProcessUrl() {
		return processUrl;
	}

	public void setProcessUrl(String processUrl) {
		this.processUrl = processUrl;
	}

	public String getLogUrl() {
		return logUrl;
	}

	public void setLogUrl(String logUrl) {
		this.logUrl = logUrl;
	}

/*	public String getSystemConfigUrl() {
		return systemConfigUrl;
	}

	public void setSystemConfigUrl(String systemConfigUrl) {
		this.systemConfigUrl = systemConfigUrl;
	}*/

	public String getDictionaryManagerUrl() {
		return dictionaryManagerUrl;
	}

	public void setDictionaryManagerUrl(String dictionaryManagerUrl) {
		this.dictionaryManagerUrl = dictionaryManagerUrl;
	}

	public SundayAreaMapper getAreaMapper() {
		return areaMapper;
	}

	public void setAreaMapper(SundayAreaMapper areaMapper) {
		this.areaMapper = areaMapper;
	}

	public String getAppManagerUrl() {
		return appManagerUrl;
	}

	public void setAppManagerUrl(String appManagerUrl) {
		this.appManagerUrl = appManagerUrl;
	}

	public String getPayConfigUrl() {
		return payConfigUrl;
	}

	public void setPayConfigUrl(String payConfigUrl) {
		this.payConfigUrl = payConfigUrl;
	}

	public static String getSystemMenuSuper() {
		return SYSTEM_MENU_SUPER;
	}

	public static String getSystemConfigSuper() {
		return SYSTEM_CONFIG_SUPER;
	}

	public static String getFaDesktop() {
		return FA_DESKTOP;
	}

	public static String getFaCog() {
		return FA_COG;
	}

	public String getAdvertisementUrl() {
		return advertisementUrl;
	}

	public void setAdvertisementUrl(String advertisementUrl) {
		this.advertisementUrl = advertisementUrl;
	}

	public String getWeixinConfigUrl() {
		return weixinConfigUrl;
	}

	public void setWeixinConfigUrl(String weixinConfigUrl) {
		this.weixinConfigUrl = weixinConfigUrl;
	}
}
